﻿using Microsoft.AspNetCore.Mvc.Rendering;
using PlatformNetCore.Common;
using PlatformNetCore.Entity;
using PlatformNetCore.Entity.Models;
using PlatformNetCore.IRepository;
using PlatformNetCore.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace PlatformNetCore.Service
{
    public class AdSlotService : IAdSlotService
    {
        private readonly IAdSlotRepository adSlotRepository;
        public AdSlotService(IAdSlotRepository _adSlotRepository)
        {
            adSlotRepository = _adSlotRepository;
        }

        #region 广告位
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LayPager<AdSlot> GetAdSlotPagedList(int pageIndex, int pageSize, Expression<Func<AdSlot, bool>> predicate)
        {
            var list = adSlotRepository.GetPagedList(pageIndex, pageSize, predicate);
            LayPager<AdSlot> pmodel = new LayPager<AdSlot>();
            if (list != null)
            {
                pmodel.count = list.TotalItemCount;
                pmodel.msg = CommonConst.Success;
                pmodel.data = list.ToList();
            }
            return pmodel;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<AdSlot> GetAdSlot(string id)
        {
            ResultResponse<AdSlot> resultResponse = new ResultResponse<AdSlot>() { success = false, msg = CommonConst.Fail };
            resultResponse.data = adSlotRepository.GetById(id);
            if (resultResponse.data != null)
            {
                resultResponse.success = true;
                resultResponse.msg = CommonConst.Success;
            }
            return resultResponse;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse AddAdSlot(AdSlot model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            if (string.IsNullOrWhiteSpace(model.AdSlotCode))
            {
                response.msg = "代码不能为空，且必须唯一!";
                return response;
            }
            var adSlot = adSlotRepository.GetByExpression(k => k.AdSlotCode == model.AdSlotCode && k.IsDel == 0);
            if (adSlot != null)
            {
                response.msg = "代码已经存在，请更换一个!";
                return response;
            }
            model.ID = IdentityCreator.NextIdentity;
            model.AddDate = DateTime.Now;
            response.success = adSlotRepository.Add(model);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse UpdateAdSlot(AdSlot model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            if (string.IsNullOrWhiteSpace(model.AdSlotCode))
            {
                response.msg = "代码不能为空，且必须唯一!";
                return response;
            }
            var o = adSlotRepository.GetById(model.ID);
            
            if (!model.AdSlotCode.Equals(o.AdSlotCode))
            {
                var adSlot = adSlotRepository.GetByExpression(k => k.AdSlotCode == model.AdSlotCode && k.IsDel == 0);
                if (adSlot != null)
                {
                    response.msg = "代码已经存在，请更换一个!";
                    return response;
                }
            }
            o.Title = model.Title;
            o.AdSlotCode = model.AdSlotCode;
            o.IsAvailable = model.IsAvailable;
            o.Remark = model.Remark;
            o.SortNo = model.SortNo;
            o.UpdateBy = model.UpdateBy;
            o.UpdateDate = DateTime.Now;

            response.success = adSlotRepository.Update(o);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 推荐，取消推荐 操作
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="dataStatus"></param>
        /// <returns></returns>
        public ResultResponse UpdateAdSlotState(List<string> ids, string userName, DataStatus dataStatus)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            var products = adSlotRepository.GetList(s => ids.Contains(s.ID)).ToList();
            if (products != null && products.Count > 0)
            {
                switch (dataStatus)
                {
                    case DataStatus.Deleted:
                        products.ForEach(k =>
                        {
                            k.IsDel = 1;
                            k.UpdateBy = userName;
                            k.UpdateDate = DateTime.Now;
                        });
                        response.success = adSlotRepository.Update(products);
                        break;
                }
                response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            }
            return response;
        }

        /// <summary>
        /// 获取当前列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SelectListItem> GetAdSlotListDLL(bool isSelect = false, bool isAll = false)
        {
            List<SelectListItem> itemlist = new List<SelectListItem>();
            IEnumerable<AdSlot> list = adSlotRepository.GetList(k => k.IsAvailable == 1 && k.IsDel == 0);
            if (list != null && list.Any())
            {
                itemlist = list.Select(p => new SelectListItem()
                {
                    Value = p.ID,
                    Text = p.Title
                }).ToList();
            }
            if (isSelect)
            {
                itemlist.Insert(0, new SelectListItem() { Value = string.Empty, Text = "--请选择--" });
            }
            if (isAll)
            {
                itemlist.Insert(0, new SelectListItem() { Value = string.Empty, Text = "--全部--" });
            }
            return itemlist;
        }
        #endregion
    }
}
